package ccot.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class ImportQuestions {
	
	public static void run(String inFilename) throws SQLException {
		DBConnManager dbConnManager = new DBConnManager();
		Connection conn=dbConnManager.getConnection("mysql");
		
		String[] rows = Tools.read(inFilename, "GBK").split("\n");
		for (String row : rows) {
			String[] cols = row.split("\t");
			String type = "";
			if (cols[1].equals("1")) {
				type = "判断题";
			} else if (cols[1].equals("2")) {
				type = "单选题";
			} else if (cols[1].equals("3")) {
				type = "多选题";
			}
			
			String chapter = cols[2];
			String difficulty = cols[3];
			String answer = cols[4];
			String question = cols[5];
			String choiceA = cols.length > 6 ? cols[6] : "";
			String choiceB = cols.length > 7 ? cols[7] : "";
			String choiceC = cols.length > 8 ? cols[8] : "";
			String choiceD = cols.length > 9 ? cols[9] : "";
			String choiceE = cols.length > 10 ? cols[10] : "";
			String courseId = "1";
			
			if (conn != null) {
				Statement stmt = conn.createStatement();
				String sql = "INSERT INTO questions (question, type, choice_a, choice_b, choice_c, choice_d, choice_e, choice_answer, course_id, chapter, difficulty) VALUES('" +
						question + "','" +
						type + "','" +
						choiceA + "','" +
						choiceB + "','" +
						choiceC + "','" +
						choiceD + "','" +
						choiceE + "','" +
						answer + "','" +
						courseId + "','" +
						chapter + "','" +
						difficulty + "')";
				System.out.println(sql);
				stmt.executeUpdate(sql);
			}
		}
		
		if (conn != null) {
			dbConnManager.releaseConnection("mysql",conn);
			dbConnManager.closeConns();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String inFilename = args[0];
		
		try {
			ImportQuestions.run(inFilename);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("done.");

	}

}
